package com.yinNuo.collectWool.quartz.task;

import com.yinNuo.collectWool.quartz.entity.CustomJob;
import com.yinNuo.collectWool.quartz.service.CustomJobService;
import com.yinNuo.collectWool.quartz.util.StringUtils;
import com.yinNuo.collectWool.utils.QuartzHttpUtils;
import com.yinNuo.collectWool.utils.Register;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Set;

/**
 * create by: yinNuo
 * create at: 2022/1/8 下午7:09
 * description: 定时任务调度测试
 */
@Slf4j
@Component("collectwoolScheduled")
public class TaskScheduled {

    @Autowired
    QuartzHttpUtils quartzHttpUtils;
    @Autowired
    CustomJobService customJobService;


    public void ryMultipleParams(String s, Boolean b, Long l, Double d, Integer i) {
        System.out.println(StringUtils.format("执行多参方法： 字符串类型{}，布尔类型{}，长整型{}，浮点型{}，整形{}", s, b, l, d, i));
    }

    public void ryParams(String params) {
        System.out.println("执行有参方法：" + params);
    }

    public void ryNoParams() {
        System.out.println("执行无参方法");
    }

    /**
     * 执行的定时任务
     * @param taskId 做任务的任务id，绑定请求相关信息
     */
    public void taskScheduled(String taskId){
        long tsId = Long.parseLong(taskId);
        Set<CustomJob> set = Register.set;
        CustomJob customJob = new CustomJob();
        for (CustomJob cusJob : set) {
            if (cusJob.getId() == tsId) {
                customJob = cusJob;
            }
        }
        //如果缓存中没有则从数据库中取
        if (null==customJob.getId()) {
            customJob = customJobService.getById(taskId);
        }
        //requesttype：0-get 1-post 2-put 3-delete
        Integer requesttype = customJob.getRequesttype();
        if (requesttype == null) {
            //无请求方式，即未找到相关任务id
            log.info("任务id为{}的任务不存在", taskId);
        } else if (requesttype ==0){
            String post = quartzHttpUtils.get(customJob.getUrl(),customJob.getUrlparam(),customJob.getHeader());
            log.info("请求成功，任务结果：{}", post);
        } else if (requesttype == 1) {
            String post = quartzHttpUtils.post(customJob.getUrl(),customJob.getUrlparam(),customJob.getHeader(),customJob.getRequestparam());
            log.info("请求成功，任务结果：{}", post);
        }
    }

}
